package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DBPack.BusStationDetail;
import DBPack.RailStation;
import Drivers.BusDriver;
import Drivers.RailDriver;

/**
 * Servlet implementation class getRailStationList
 */
public class getRailStationList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getRailStationList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONArray x=new JSONArray();
		String railCode=request.getParameter("rail_code");
		RailStation[] list=RailDriver.getRailStationList(railCode);

	    response.setContentType("text/html; charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		
		JSONObject y; 	
		for (int i=0;i<list.length;i++) {
			y=new JSONObject();
			try {			
				y.put("railStationId", list[i].getRailStationId());
				y.put("railCode",list[i].getRailCode());
				y.put("stationName", list[i].getStationName());
				y.put("stationOrder", list[i].getStationOrder());
				x.put(y);
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
		out.print(x);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
